function isPoneAvailable(str) {
    var reg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!reg.test(str)) {
        return false;
    } else {
        return true;
    }
}

var countdown = 60;
// 从cookie里面获取发送code时间
var value = getCookie("send-countdown");
if (value > 0) { // 还没有过60S
    $("#send-code").addClass("layui-btn-disabled");
    $("#send-code").text("重新发送(" + value + ")S");
    countdown = value;
    setTime($("#send-code"));
}

$("#send-code").click(function() {
    var $obj = $(this);
    var $phone = $("#user-login-phone").val();
    if (null == $phone || undefined == $phone || "" == $phone) {
        layer.msg("请输入电话号码!");
        return false;
    }
    if (!isPoneAvailable($phone)) {
        layer.msg("电话号码格式不正确!");
        return false;
    }

    var value = getCookie("send-countdown");
    if (value > 0) {
        return false;
    }

    setTime($obj);
    sendCode();
    return false;
});


function setTime(obj) {
    if (countdown == 0) {
        obj.removeClass("layui-btn-disabled");
        obj.text("发送验证码");
        setCookie("send-countdown", countdown);
        countdown = 60;
        return;
    } else {
        obj.addClass("layui-btn-disabled");
        obj.text("重新发送(" + countdown + ")S");
        countdown--;
        setCookie("send-countdown", countdown);
    }
    setTimeout(function () {
        setTime(obj);
    }, 1000)
}

function setCookie(name, value){
    var date = new Date();   //初始化时间
    date.setTime(date.getTime() + 1 * 60 * 1000);   //时间单位毫秒
    document.cookie = name+"="+value+";expires="+date.toGMTString()+";path=/";

}

function getCookie(name) {
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(name + "=")
        //如果document.cookie对象里面有cookie则查找是否有指定的cookie，如果有则返回指定的cookie值，如果没有则返回空字符串
        if (c_start!=-1){
            c_start=c_start + name.length+1
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
        }
    }
    return 0;
}

function sendCode() {
    layer.msg("发送短信!")
    return;
}